package com.yunhoo.pay.db.impl;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.yunhoo.pay.model.Expense;
import com.yunhoo.pay.model.QueryVouchers;
import com.yunhoo.pay.util.BillNumBuilder;
import com.yunhoo.pay.util.BillOrder;
import com.yunhoo.sys.db.util.IbatisTemplate;
import com.yunhoo.sys.util.common.LoginParam;
import com.yunhoo.sys.util.common.Null;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LoanBillImpl {

    private static String nameSpace = "loanbill.";

    private static Map paramMap() {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        return map;
    }

    public static void insertLoanBillVoucher(Expense exp) {
        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();

            exp.setNumber(BillNumBuilder.buildNum(client));

            Map map = paramMap();
            map.put("voutype", exp.getVoutype());
            map.put("numDate", exp.getNumDate());
            //map.put("number", getMaxNumber(client));
            map.put("number", exp.getNumber());
            map.put("projCode", Null.convert(exp.getProject().getFinCode()));
            map.put("affair", exp.getAffair());
            map.put("total", exp.getTotal());
            map.put("numberStatus", exp.getNumberStatus());
            map.put("person", exp.getUser().getCpsn_number());
            map.put("deptcode", exp.getDept().getCode());
            map.put("memo", exp.getMemo());
            map.put("payStyle", exp.getPayStyle().getCode().trim());
            map.put("payDept", exp.getPayDept().getCode());
            map.put("feePerson", exp.getFeePer().getCpsn_number());
            map.put("isQuota", exp.getIsQuota());
            map.put("affixAmount", exp.getAffixAmount());
            client.insert(nameSpace + "insertLoanBillVoucherEmp", map);

            client.commitTransaction();
        } catch (SQLException ex) {
            Logger.getLogger(LoanBillImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(LoanBillImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public static String getMaxNumber(SqlMapClient client) {
        String ExpenseMaxNumber = (String) IbatisTemplate.query("loanbill.maxExpenseNumber", LoginParam.getOrg().getCode().trim());
        if (ExpenseMaxNumber == null || ExpenseMaxNumber.length() == 0) {
            ExpenseMaxNumber = "000001";
            return ExpenseMaxNumber;
        }
        ExpenseMaxNumber = String.format("%06d%n", Integer.parseInt(ExpenseMaxNumber.trim()) + 1);
        return ExpenseMaxNumber.trim();
    }

    public static void updateLoanBill(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numDate", exp.getNumDate());
        map.put("projCode", Null.convert(exp.getProject().getFinCode()));
        map.put("affair", exp.getAffair());
        map.put("total", exp.getTotal());
        map.put("memo", exp.getMemo());
        map.put("numberStatus", exp.getNumberStatus());
        map.put("payDept", exp.getPayDept().getCode());
        map.put("feePerson", exp.getFeePer().getCpsn_number());
        map.put("affixAmount", exp.getAffixAmount());
        map.put("voutype", exp.getVoutype());
        IbatisTemplate.update(nameSpace + "updateLoanBill", map);
    }

    public static void updateLoanBillForAudit(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("voutype", exp.getVoutype());
        map.put("numstatus", exp.getNumberStatus());
        map.put("checkaccount", exp.getCheckaccount());
        map.put("remain", exp.getRemain());
        map.put("budgetsource", exp.getBudgetSource());
        map.put("eoc", exp.getCostitem().getCode());
        map.put("paycode", exp.getPaycode());
        map.put("costclass", exp.getCostClass());
        map.put("costtype", exp.getCostTypes().getCode());
        map.put("auditor", exp.getAuditor());
        map.put("auditdate", exp.getAuditDate());
        IbatisTemplate.update(nameSpace + "updateLoanBillForAudit", map);
    }

    public static void updateLoanBillForCheck(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("numstatus", exp.getNumberStatus());
        map.put("checkaccount", exp.getCheckaccount());
        map.put("budgetsource", exp.getBudgetSource());
        map.put("eoc", exp.getCostitem().getCode());
        map.put("paycode", exp.getPaycode());
        map.put("costclass", exp.getCostClass());
        map.put("costtype", exp.getCostTypes().getCode());
        map.put("auditor", exp.getAuditor());
        map.put("auditdate", exp.getAuditDate());
        map.put("voutype", exp.getVoutype().trim());
        IbatisTemplate.update(nameSpace + "updateLoanBillForCheck", map);
    }

    public static void updateLoanBillForPay(Expense exp) {
        Map map = paramMap();
        map.put("number", exp.getNumber());
        map.put("voutype", exp.getVoutype().trim());
        map.put("numstatus", exp.getNumberStatus());
        map.put("payaccount", exp.getPayaccount());
        map.put("payStyle", exp.getPayStyle().getCode());
        map.put("ticketcode", exp.getTicketcode());
        map.put("payer", exp.getPayer());
        map.put("paydate", exp.getPayDate());
        map.put("cashPaid", exp.getCashPaid());
        map.put("receive", exp.getReceive());
        map.put("repayment", Null.convert(exp.getRepayment()));
        IbatisTemplate.update(nameSpace + "updateLoanBillForPay", map);
    }

    private static Map conditionMap(QueryVouchers qlb) {
        Map map = new HashMap();
        map.put("orgCode", LoginParam.getOrg().getCode());
        map.put("number", qlb.getNumber().trim());
        map.put("affair", qlb.getAffair().trim());
        map.put("projcode", Null.convert(qlb.getProject().getFinCode()).trim());
        map.put("deptname", Null.convert(qlb.getDept().getName()).trim());
        map.put("cpsn_name", qlb.getUser().getCpsn_number().trim());
        map.put("budgetSource", qlb.getBudgetSource().trim());
        map.put("costcode", Null.convert(qlb.getCostitem().getCode()).trim());
        map.put("paycode", qlb.getPaycode().trim());
        map.put("costClass", qlb.getCostClass().trim());
        map.put("costType", qlb.getCostTypes().getCode().trim());
        map.put("payStyle", qlb.getPayStyle().trim());
        map.put("ticketcode", qlb.getTicketcode().trim());
        map.put("numDateStart", qlb.getNumDateStart());
        map.put("numDateEnd", qlb.getNumDateEnd());
        map.put("auditDateStart", qlb.getAuditDateStart());
        map.put("auditDateEnd", qlb.getAuditDateEnd());
        map.put("payDateStart", qlb.getPayDateStart());
        map.put("payDateEnd", qlb.getPayDateEnd());
        map.put("totalStart", qlb.getTotalStart());
        map.put("totalEnd", qlb.getTotalEnd());
        map.put("checkaccountStart", qlb.getCheckaccountStart());
        map.put("checkaccountEnd", qlb.getCheckaccountEnd());
        map.put("payaccountStart", qlb.getPayaccountStart());
        map.put("payaccountEnd", qlb.getPayaccountEnd());
        map.put("sql", Null.convert(qlb.getStatus()).trim());
        return map;
    }

    public static List<Expense> queryListCheck(QueryVouchers qlb) {
        Map map = conditionMap(qlb);
        map.put("billOrder", BillOrder.checkOrder);//单据排列顺序
        return IbatisTemplate.queryForList(nameSpace + "loanbillquery", map);
    }

    public static List<Expense> queryListPay(QueryVouchers qlb) {
        Map map = conditionMap(qlb);
        map.put("billOrder", BillOrder.payOrder);//单据排列顺序
        map.put("billRange", "'2','4','6'");
        return IbatisTemplate.queryForList(nameSpace + "loanbillquery", map);
    }

    public static void main(String args[]) {

        SqlMapClient client = IbatisTemplate.getSqlMapClient();
        try {
            client.startTransaction();
            client.startBatch();
            for (int i = 1; i <= 100000; i++) {
                String str1 = "value " + String.valueOf(i);
                String str2 = "value " + String.valueOf(i);
                Map map = new HashMap();
                map.put("col1", str1);
                map.put("col2", str2);
                client.insert("loanbill.insertTest", map);
                if (i % 500 == 0) {
                    System.out.println("提交 " + i);
                    client.executeBatch();
                    client.commitTransaction();
                    client.startBatch();
                }
            }
            client.executeBatch();
            client.commitTransaction();

        } catch (SQLException ex) {
            Logger.getLogger(LoanBillImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                client.endTransaction();
            } catch (SQLException ex) {
                Logger.getLogger(LoanBillImpl.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
}
